﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace controlWin
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

           
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT //定位当前窗口位置
        {
            public int x;                             //最左坐标
            public int y;                             //最上坐标

        }

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);

       

        /// <summary>
        /// 设置目标窗体大小，位置
        /// </summary>
        /// <param name="hWnd">目标句柄</param>
        /// <param name="x">目标窗体新位置X轴坐标</param>
        /// <param name="y">目标窗体新位置Y轴坐标</param>
        /// <param name="nWidth">目标窗体新宽度</param>
        /// <param name="nHeight">目标窗体新高度</param>
        /// <param name="BRePaint">是否刷新窗体</param>
        /// <returns></returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool BRePaint);

        [DllImport("USER32.DLL")]
        public static extern void SwitchToThisWindow(IntPtr hwnd, Boolean fAltTab);

        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        RECT rect = new RECT();
        //开始
        private void button1_Click(object sender, EventArgs e)
        {

           

            Thread thread = new Thread(new ThreadStart(Pck));
            thread.Start();
        }



        public void testPck() { 
        
            List<String> list = new List<string> ();
            for (int i = 0; i < 20; i++) {
                list.Add(String.Format("{0}",i));
            }

            //当前的屏幕除任务栏外的工作域大小
            int w = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
            int h = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;

            Console.WriteLine("w=" + (w / 6));
            Console.WriteLine("h=" + (h / 3));



            int index = 0;

            int row = 3;
            int column = 6;

            int anyDeskWidth = w / column;
            int anyDeskHeight = h / row;

            int curentRow = 0;//当前行
            foreach (var item in list)
            {

                //获取当前行
                int total = index + 1;
                curentRow = total % column == 0 ? (total / column) : (total / column) + 1;
                if (curentRow - 1 <= 0)
                {
                    curentRow = 0;
                }
                else {
                    curentRow -=1;
                }

                //获取当前列
                int currentColumn = column - (curentRow + 1) * column + index;

                Console.WriteLine("curentRow = " + curentRow);
                Console.WriteLine("currentColumn = " + currentColumn);

                // GetWindowRect(item, ref rect);//获取窗口在屏幕上的坐标
                //MoveWindow(item, index * anyDeskWidth, curentRow * anyDeskHeight, anyDeskWidth, anyDeskHeight, false); //锁定为1024*768分辨率
                index++;
            }

        }

        //禁止修改窗口
        public void Pck()
        {
            //当前的屏幕除任务栏外的工作域大小
            int w = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
            int h = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;

            Console.WriteLine("w=" + (w / 6));
            Console.WriteLine("h=" + (h / 3));



            int index = 0;

            int row = 3;
            int column = 6;

            int anyDeskWidth = w / column;
            int anyDeskHeight = h / row;

            int curentRow = 0;//当前行
                              // List<IntPtr> list = WindowsEnumerator.GetWindowHandles("AnyDesk");
            List<IntPtr> list = GetProgressHandler();
            foreach (var item in list)
            {

                //获取当前行
                int total = index + 1;
                curentRow = total % column == 0 ? (total / column) : (total / column) + 1;
                if (curentRow - 1 <= 0)
                {
                    curentRow = 0;
                }
                else
                {
                    curentRow -= 1;
                }
                //获取当前列
                int currentColumn = column - (curentRow + 1) * column + index;

                Console.WriteLine("curentRow = "+ curentRow);
                Console.WriteLine("currentColumn = " + currentColumn);

                //SwitchToThisWindow(item, true);
                //SetForegroundWindow(item);
                // GetWindowRect(item, ref rect);//获取窗口在屏幕上的坐标
                MoveWindow(item, currentColumn * anyDeskWidth, curentRow * anyDeskHeight, anyDeskWidth, anyDeskHeight, false); //锁定为1024*768分辨率
                index++;
            }

            /**
            while (true)
            {
                foreach (var item in WindowsEnumerator.GetWindowHandles("AnyDesk"))
                {
                    GetWindowRect(item, ref rect);//获取窗口在屏幕上的坐标
                    MoveWindow(item, 0, 0, 320, 346, true); //锁定为1024*768分辨率
                }
                Thread.Sleep(2000); //2秒检测一次
            }
            **/
        }

       //获取已打开进程的句柄
        private List<IntPtr> GetProgressHandler() {

            List<IntPtr> list = new List<IntPtr>();
            foreach (Process p in Process.GetProcesses())
            {
                try
                {
                    if (p.MainWindowHandle != IntPtr.Zero && p.ProcessName.Equals("AnyDesk"))
                    {
                        string str = p.MainModule.FileName;

                        Console.WriteLine("ProcessName=" + p.ProcessName);
                        // Display the user name of the program
                        Console.WriteLine("name=" + str); //string s = p.ToString ();
                        SwitchToThisWindow(p.MainWindowHandle, true);
                        //SetForegroundWindow(p.MainWindowHandle);
                        list.Add(p.MainWindowHandle);
                    }
                }
                catch
                {
                }

            }
            return list;
        }
    }
}
